package org.truenewx.tnxjeex.unit.jackson;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.truenewx.tnxjeex.unit.model.MeasureUnit;

public class MeasureUnitSerializer<U extends MeasureUnit<?>> extends StdSerializer<U> {

    public MeasureUnitSerializer(Class<U> valueClass) {
        super(valueClass, false);
    }

    @Override
    public void serialize(U unit, JsonGenerator generator, SerializerProvider provider)
            throws IOException {
        generator.writeString(unit.getKey());
    }

    @Override
    public void serializeWithType(U unit, JsonGenerator generator, SerializerProvider provider,
            TypeSerializer typeSerializer) throws IOException {
        // 不生成类型字段
        serialize(unit, generator, provider);
    }

}
